<?php
/**
 * 礼物
 * 
 * Created	: 2011-05-18
 * Modified : 2011-05-18
 * @author  : 陈立强
 */
class Gift
{
	
	public static $cfg  = null;
	public static $cfgName = 'item/gift';
	
	/**
	 * 获取信息
	 * @param int $uid
	 * @param int $target_uid
	 */
	public static function getPresentList($uid)
	{
		global $DB, $L, $G;
		
		$list = C('item/gift');
		
		return array(
			'val' => true,
			'data' => $list
		);
	}
	
	/**
	 * 赠送礼物
	 * @param int $uid
	 * @param array $friendList
	 * @param int $item_id
	 */
	public static function present($uid, $friendList, $item_id)
	{
		global $DB, $L, $G;
		
		$today = strtotime('today');
		$now = time();
		
		if (is_int_string($friendList)) {
			$friendList = array($friendList);
		} else if (!is_array($friendList)) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		
		$itemList = C('item/gift');
		$itemCfg = $itemList[$item_id];
		if (!$itemCfg) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		
		$friendIdStr = join(',', $friendList);
		
		// 判断是否好友
		$where = '(uid='.$uid.' OR to_uid='.$uid.') and status=1';
		$sql = 'SELECT uf_id,uid,to_uid,present_time,to_present_time FROM user_friend WHERE '.$where;
		$list = $DB->getList($sql);
		$allFriend = array();
		// 同时更新赠送时间
		$updateSql = '';
		foreach ($list as $v) {
			if ($v['uid'] == $uid) {
				$allFriend[] = $v['to_uid'];
				if (!in_array($v['to_uid'], $friendList)) {
					continue;
				}
				// 同一个好友只能赠送一次
				if ($v['present_time'] >= $today) {
					return array(
						'val' => false,
						'msg' => $L['same_friend_only_one_present_times']
					);
				}
				$updateSql .= 'UPDATE user_friend SET present_time='.$now.' WHERE uf_id='.$v['uf_id'].';';
			} else {
				$allFriend[] = $v['uid'];
				if (!in_array($v['uid'], $friendList)) {
					continue;
				}
				// 同一个好友只能赠送一次
				if ($v['to_present_time'] >= $today) {
					return array(
						'val' => false,
						'msg' => $L['same_friend_only_one_present_times']
					);
				}
				$updateSql .= 'UPDATE user_friend SET to_present_time='.$now.' WHERE uf_id='.$v['uf_id'].';';
			}
		}
		$noFriend = true;
		foreach ($friendList as $u) {
			if (!in_array($u, $allFriend)) {
				$noFriend = false;
				break;
			}
		}
		if (!$noFriend) {
			return array(
				'val' => false,
				'msg' => $L['not_friend_in_list']
			);
		}
		
		$where = 'uid='.$uid;
		$sql = 'SELECT lvl,present_times,last_present_time FROM user WHERE '.$where;
		$user = $DB->getValue($sql);
		// 牧场等级不足
		if ($user['lvl'] < $itemCfg['need_mc_lvl']) {
			return array(
				'val' => false,
				'msg' => $L['mc_lvl_not_enough']
			);
		}
		// 赠送次数已满
		if ($user['last_present_time'] >= $today && $user['present_times'] <= 0) {
			return array(
				'val' => false,
				'msg' => $L['present_time_not_enough']
			);
		} else if ($user['last_present_time'] < $today) {// 今天第一次赠送
			$present_times = $G['present_gift_times_perday'] - 1;
		} else {//今天赠送次数减1
			$present_times = $user['present_times'] - 1;
		}
		
		$DB->beginTransaction();
		$ret = true;
		
		// 更新赠送信息
		$data = array(
			'present_times'		=> $present_times,
			'last_present_time'	=> $today
		);
		$ret = $DB->update('user', $data, $where);
		
		// 更新赠送时间
		if ($ret) {
			$ret = $DB->exeSql($updateSql);
		}
		
		// 物品进入好友道具箱
		if ($ret) {
			foreach ($friendList as $to_uid) {
				$data = array(
					'uid'		=> $to_uid,
					'item_id'	=> $item_id,
					'num'		=> 1,
					'title'		=> $itemCfg['title'],
					'lvl'		=> $itemCfg['lvl']
				);
				$return = PropsBox::in($to_uid, $data);
				$ret = $return['val'];
				if ($ret) {
					$data = array(
						'uid'		=> $uid,
						'to_uid'	=> $to_uid,
						'item_id'	=> $item_id,
						'title'		=> $itemCfg['title'],
						'present_time'	=> $now,
					);
					$ret = $DB->insert('log_present_gift', $data);
					if (!$ret) {
						break;
					}
				} else {
					break;
				}
			}
		}
	
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
			$result['data'] = array(
				'present_time' => $present_times
			);
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('gift_present', $str);
		}
		
		return $result;
	}
	
	/**
	 * 获赠礼物列表
	 * @param int $uid
	 */
	public static function getPresentedList($uid) 
	{
		global $DB, $L, $G;
		
		// 获取最新可保存记录条数的最早时间
		$sql = 'SELECT MIN(`present_time`) FROM log_present_gift
				WHERE to_uid='.$uid.' ORDER BY id DESC LIMIT '.$G['save_presented_rows'];
		$time = $DB->getValue($sql);
		// 删除可保存记录条数最早时间之前的数据
		$sql = 'DELETE FROM log_present_gift WHERE present_time<'.$time;
		$DB->exeSql($sql);
		
		$sql = 'SELECT * FROM log_present_gift WHERE to_uid='.$uid;
		$list = $DB->getList($sql);
		
		// 获取所有赠送的好友ID
		$uidList = array();
		foreach ($list as $v) {
			$uidList[] = $v['uid'];
		}
		// 获取所有赠送好友的昵称
		$sql = 'SELECT uid,nickname FROM user WHERE uid IN ('.join(',', $uidList).')';
		$tmp = $DB->getList($sql);
		// 关联UID和昵称
		$nicknameList = array();
		foreach ($tmp as $v) {
			$nicknameList[$v['uid']] = $v['nickname'];
		}
		// 添加记录中赠送好友的昵称
		foreach ($list as $key => $value) {
			$list[$key]['nickname'] = $nicknameList[$value['uid']];
		}
		
		return array(
			'val' => true,
			'data' => $list
		);
		
	}
	
}










